package com.mezz.bones.framwork.notification.email.impl;

import cn.hutool.core.lang.Validator;
import com.mezz.bones.framework.base.domain.enums.ResponseCode;
import com.mezz.bones.framework.base.exception.BaseException;
import com.mezz.bones.framwork.notification.email.EmailService;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;

/**
 * @Author: zhuang
 * @Date: 2024/6/6 10:15
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
@Slf4j
public class TxEmailService implements EmailService {

    @Resource
    private JavaMailSender mailSender;

    public void sendEmailWithText(String to, String subject, String text) {

        if (!Validator.isEmail(to)) {
            throw new BaseException(ResponseCode.DATA_FORMAT_ERROR, "请填写正确的邮箱地址!");
        }

        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom("WeChat@laicui.com");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);

            mailSender.send(message);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("sendEmailWithText error:{}", e.getMessage());
        }
    }

    public void sendEmailWithAttachment(String to, String subject, String text, String fileName, InputStream inputStream, String contentType) {

        if (!Validator.isEmail(to)) {
            throw new BaseException(ResponseCode.DATA_FORMAT_ERROR, "请填写正确的邮箱地址!");
        }

        try {

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom("WeChat@laicui.com");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);

            ByteArrayResource resource = new ByteArrayResource(inputStream.readAllBytes());
            helper.addAttachment(fileName, resource, contentType);
            mailSender.send(message);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("sendEmailWithText error:{}", e.getMessage());
        }
    }



    public void sendEmailWithAttachment(String to, String subject, String content, File file) {

        if (!Validator.isEmail(to)) {
            throw new BaseException(ResponseCode.DATA_FORMAT_ERROR, "请填写正确的邮箱地址!");
        }

        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom("WeChat@laicui.com");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            helper.addAttachment(file.getName(), file);
            mailSender.send(message);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("sendEmailWithText error:{}", e.getMessage());
        }
    }
}
